package com.lagou.hbase;


import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;


import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;


/**
 * 好友删除
 */
public class FriendProcessor extends BaseRegionObserver {
	@Override
	public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
		HTableWrapper table = (HTableWrapper) e.getEnvironment().getTable(TableName.valueOf("user"));
		//获取列族
		final NavigableMap<byte[], List<Cell>> familyCellMap = delete.getFamilyCellMap();
		final Set<Map.Entry<byte[], List<Cell>>> entries = familyCellMap.entrySet();

		//遍历列族
		for (Map.Entry<byte[], List<Cell>> entry : entries) {
			//获取到所有的列
			final List<Cell> value = entry.getValue();
			for (Cell cell : value) {
				//获取rowkey
				byte[] rowkey = CellUtil.cloneRow(cell);
				//获取列名
				final byte[] qualifier = CellUtil.cloneQualifier(cell);

				//删除对象
				final Delete delete1 = new Delete(qualifier).addColumn(Bytes.toBytes("friend"), rowkey);
				table.delete(delete1);


			}

		}

		table.close();
	}
}
